package com.example.administrator.capital.consult;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.capital.MainFragment;
import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.CommentAdapter;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.item.ItemDetailFragment;
import com.example.administrator.capital.mine.second.NewDetailFragment;
import com.example.administrator.capital.model.TestBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import layout.LoadingLayout;
import recycle.BaseViewHolder;

/**
 * A simple {@link Fragment} subclass.
 * 推荐
 */
public class RecommendFragment extends BaseBackFragment {

    @BindView(R.id.recycle_view)
    RecyclerView mRecycleView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.loading)
    LoadingLayout mLoading;
    Unbinder unbinder;
    private CommentAdapter<TestBean> mAdapter;
    private List<TestBean> mTestBeans = new ArrayList<>();

    public RecommendFragment() {
        // Required empty public constructor
    }

    public static RecommendFragment newInstance() {

        Bundle args = new Bundle();
        RecommendFragment fragment = new RecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mSwipeLayout.setColorSchemeResources(R.color.font_blue);
        mAdapter = new CommentAdapter<TestBean>(R.layout.recommend_item_list, null) {
            @Override
            public void setViewData(BaseViewHolder holder, TestBean item, int position) {
                holder.setGone(R.id.relative, item.isTop()).setGone(R.id.layout_bom,!item.isTop());
            }

            @Override
            public void setEvent(BaseViewHolder holder, TestBean item, final int position) {

                holder.setOnClickListener(R.id.layout, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((MainFragment) getParentFragment().getParentFragment()).startBrotherFragment(ConsultDetailFragment.newInstance());

                    }
                });
            }
        };
        mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration div = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        div.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider));
        mRecycleView.addItemDecoration(div);
        mRecycleView.setAdapter(mAdapter);
    }

    @Override
    protected void setListener() {
        super.setListener();
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }

    @Override
    protected void setData() {
        super.setData();
        getData();
    }
    private void getData() {
        mTestBeans.clear();
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                mTestBeans.add(new TestBean(true));
            } else {
                mTestBeans.add(new TestBean(false));
            }

        }
        mAdapter.setNewData(mTestBeans);
        mLoading.showContent();
        mSwipeLayout.setRefreshing(false);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
